package com.explore.demo.config;

import com.explore.demo.intercept.TokenInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

import javax.annotation.Resource;
import java.io.File;

/**
 * @author gaolainan
 */
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer
{

    @Value("${file.uploadUrl}")
    private String uploadFilePath;

    @Resource
    private TokenInterceptor tokenInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // addPathPatterns 表示拦截对应页面的请求
        // excludePathPattern 不拦截的页面
        // 注册TestInterceptor拦截器
        registry.addInterceptor(tokenInterceptor).addPathPatterns("/**")
                .excludePathPatterns("/human_source/**")
                .excludePathPatterns("/comment/**")
                .excludePathPatterns("/company_category/**")
                .excludePathPatterns("/recruit_category/**")
                .excludePathPatterns("/welfare_category/**")
                .excludePathPatterns("/job/**")
                .excludePathPatterns("/index/**")
                .excludePathPatterns("/category/**")
                .excludePathPatterns("/company/**")
                .excludePathPatterns("/recruitment/**")
                .excludePathPatterns("/user/login", "/user/register","/user/login_verify","/user/verifyCode")
                .excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**");
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        // registry.addViewController("/").setViewName("index");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/")
                .addResourceLocations("classpath:/templates/");
        registry.addResourceHandler("/swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/job/**")
                .addResourceLocations("file:" + uploadFilePath + File.separator);

        WebMvcConfigurer.super.addResourceHandlers(registry);
    }

    /**
     * 解决跨域问题
     * @param registry
     */
    // @Override
    // public void addCorsMappings(CorsRegistry registry) {
    //     registry.addMapping("/**")
    //             .allowedMethods("*")
    //             .allowedOrigins("*")
    //             .allowedHeaders("*");
    //     WebMvcConfigurer.super.addCorsMappings(registry);
    // }

}
